﻿using System;
using System.Web;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Web.SessionState;

namespace IFD2
{
    public class avail_fares : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");

            //if (IsPostBack)
            //{
            //    Response.Write("* Don't post back!");
            //    Response.End();
            //    return;
            //}

            if (context.Session["ACL"].ToString().IndexOf("SA3") < 0)
            {
                context.Response.Write("* Unauthorized!");
                context.Response.End();
                return;
            }
            //Response.Write("OK");
            //Response.End();
            //return;
            Random MyRandom = new Random();
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            StringBuilder buf = new StringBuilder();
            int ret_code, i;
            DataTable dt_ret = new DataTable();
            i = Convert.ToInt16(context.Request.QueryString["seg_sn"]);
            context.Session["FLT_DATE_" + i.ToString()] = Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]).AddDays(Convert.ToSingle(context.Request.QueryString["offset"]));
            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_avail_fares";
                para = cmd.Parameters.AddWithValue("@ori_city", context.Session["ORI_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@des_city", context.Session["DES_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@lang", context.Session["LANG"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_date", Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]));
                para = cmd.Parameters.AddWithValue("@seats", Convert.ToInt16(context.Session["SEATS"]));
                para = cmd.Parameters.AddWithValue("@seg_sn", i);
                para = cmd.Parameters.AddWithValue("@seg_total", Convert.ToInt16(context.Session["TRIP_COUNT"]));
                para = cmd.Parameters.AddWithValue("@user_airline", context.Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user_office", context.Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", context.Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", context.Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd, ref dt_ret);
                if (ret_code != 0)
                {
                    if (ret_code == 1)
                    {
                        context.Response.Write("City not found!");
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                    //else if (ret_code == 2)
                    //{
                    //    context.Response.Write("Daylight saving time error!");
                    //    context.ApplicationInstance.CompleteRequest();
                    //    return;
                    //}
                    else
                    {
                        context.Response.Write("Error " + ret_code.ToString());
                        context.ApplicationInstance.CompleteRequest();
                        return;
                    }
                }
                StringBuilder tmp = new StringBuilder();
                int row_count = 0;
                buf.Append("<b>" + i.ToString() + ". <button class=\"btn btn-sm btn-default\" type=\"button\" onclick=\"query_fares(" + i.ToString() + ",-2)\">-2</button><button class=\"btn btn-sm btn-default\" type=\"button\" onclick=\"query_fares(" + i.ToString() + ",-1)\">-1</button><button class=\"btn btn-sm btn-default\" type=\"button\">" + context.Session["ORI_" + i.ToString()].ToString() + "-" + context.Session["DES_" + i.ToString()].ToString() + " "
                       + Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]).ToShortDateString() + "</button><button class=\"btn btn-sm btn-default\" type=\"button\" onclick=\"query_fares(" + i.ToString() + ",1)\">+1</button><button class=\"btn btn-sm btn-default\" type=\"button\" onclick=\"query_fares(" + i.ToString() + ",+2)\">+2</button></b><br/>");
                if (dt_ret.Rows.Count == 0)
                {
                    buf.Append("No available fares.<br/>");
                }
                else
                {
                    foreach (DataRow curr_row in dt_ret.Rows)
                    {
                        row_count++;
                        tmp.Append("fare_avail_flts.ashx?&ran=" + MyRandom.Next(0, int.MaxValue).ToString() + "&airline=" + curr_row["airline"].ToString().TrimEnd() + "&fare_code=" + curr_row["fare_code"].ToString().TrimEnd() + "&flt_date=" + Convert.ToDateTime(context.Session["FLT_DATE_" + i.ToString()]).ToShortDateString()
                            + "&ori_airport=" + curr_row["ori_airport"].ToString() + "&des_airport=" + curr_row["des_airport"].ToString() + "&cabin=" + curr_row["cabin"].ToString() + "&class=" + curr_row["class"].ToString()
                            + "&lang=" + context.Session["LANG"].ToString() + "&seats=" + context.Session["SEATS"].ToString() + "&dbf=" + curr_row["dbf"].ToString()); //+ "&international=" + curr_row["international"].ToString());
                        tmp.Append("&seg_sn=" + i.ToString() + "&seg_total=" + context.Session["TRIP_COUNT"].ToString());
                        buf.Append("<table><tr><td width='10px'></td><td><input class=\"btn btn-sm btn-default\" type='button' style='font-family:Courier' id='btn_" + i.ToString() + "_" + row_count.ToString() + "' onClick=\"QFareAvailFlts('" + i.ToString() + "','" + row_count.ToString() + "','" + tmp.ToString() + "');\" value='+'></input></td><td width='120px'>"
                            + curr_row["airline_name"].ToString() + "</td><td width='80px'>" + curr_row["cabin_name"].ToString() + "</td><td width='200px'><b>");
                        if (Convert.ToInt16(curr_row["seg_total"]) > 1)
                            buf.Append("<span style=\"background:#90f090;\">");
                        buf.Append(curr_row["fare_name"].ToString() + "(" + curr_row["fare_code"].ToString() + ")");
                        if (Convert.ToInt16(curr_row["seg_total"]) > 1)
                            buf.Append("</span>");
                        buf.Append("</b></td><td width='150px'><b>" + curr_row["currency"].ToString() + Convert.ToSingle(curr_row["adt_price"]).ToString() + "/" + Convert.ToSingle(curr_row["chd_price"]).ToString() + "/" + Convert.ToSingle(curr_row["inf_price"]).ToString()
                            + "</b></td><td width='100px'>" + curr_row["ori_apt_name"].ToString().TrimEnd() + "-" + curr_row["des_apt_name"].ToString() + "(" + curr_row["seg_sn"] + "/" + curr_row["seg_total"]
                            + ")</td><td>Days Ahead:" + curr_row["dbf"].ToString() + "</td></tr></table>");

                        buf.Append("<div id=\"fare_" + i.ToString() + "_" + row_count.ToString() + "\"></div><br/>");

                        tmp.Clear();
                    }
                }
                dt_ret.Clear();
                buf.Append("<hr/>");
                context.Response.Write(buf.ToString());
                context.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                if (context.Application["DEBUG"].ToString() == "1")
                {
                    context.Response.Write(ex.Message);
                    context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    context.Response.Write("Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.");
                    context.ApplicationInstance.CompleteRequest();
                }
                return;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}